var models = require('./connectmysql');//数据库链接信息
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('./sqlfun');//sql语句

// 连接数据库
var conn = mysql.createConnection(models.mysql);

conn.connect();

// 增加用户接口
router.post('/addUser', (req, res) => {
    var sql = $sql.user.add;
    var params = req.body;
    console.log("sql",sql);
    console.log("params",params);
    conn.query(sql, [params.username,params.userpas,params.usertel], function(err, data) {
      if(err){
        var result={
          "status":"500",
          "message":"服务器错误"
        }
        console.log(res.jsonp(result));
        return res.jsonp(result);
      }
      else {
        var result={
          "status":"200",
          "message":"success",
          data:data
        }
        console.log(res.jsonp(result));
      }
    })
  }
);
router.post('/login', (req, res) => {
    var sql = $sql.user.checklogin;
    var params = req.body;
    console.log("sql",sql);
    console.log("params",params);
    conn.query(sql, [params.username], function(err, data) {
      if(err){
        var result={
          "status":"500",
          "message":"服务器错误"
        }
        console.log(res.jsonp(result));
        return res.jsonp(result);
      }
      else {
        var result={
          "status":"200",
          "message":"success",
          data:data
        }
        console.log(res.jsonp(result));
      }
    })
  }
);
router.post('/selectbasemes', (req, res) => {
    var sql = $sql.datames.selectbasefood;
    var params = req.body;
    console.log("sql",sql);
    console.log("params",params);
    conn.query(sql, function(err, data) {
      if(err){
        var result={
          "status":"500",
          "message":"服务器错误"
        }
        console.log(res.jsonp(result));
        return res.jsonp(result);
      }
      else {
        var result={
          "status":"200",
          "message":"success",
          data:data
        }
        console.log(res.jsonp(result));
      }
    })
  }
);
module.exports = router;
